package com.me.seckill.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * The class Wrap mapper.
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Wrapper<T> {

	/** 成功码 */
	public static final int SUCCESS_CODE = 200;

	/** * 成功信息 */
	public static final String SUCCESS_MESSAGE = "success";

	/** 错误信息 */
	public static final String ERROR_MESSAGE = "fail";

	/** 编号 */
	private int code;

	/** 信息 */
	private String message;

	/** 结果 */
	private T result;

	/**
	 * Instantiates a new wrapper.
	 */
	public Wrapper(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public Wrapper(int code, String message, T result) {
		this.code = code;
		this.message = message;
		this.result = result;
	}

	/**
	 * 包装成功结，code=200
	 */
	public static <E> Wrapper<E> ok() {
		return new Wrapper<>(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
	}

	/**
	 * 包装成功结果
	 */
	public static <E> Wrapper<E> ok(E o) {
		return new Wrapper<>(Wrapper.SUCCESS_CODE, null, o);
	}

	/**
	 * 包装成功结果
	 */
	public static <E> Wrapper<E> ok(String message, E o) {
		return new Wrapper<>(Wrapper.SUCCESS_CODE, message, o);
	}

	/**
	 * 包装失败结果
	 */
	public static <E> Wrapper<E> error(int code, String message) {
		return new Wrapper<>(code, StringUtils.isBlank(message) ? Wrapper.ERROR_MESSAGE : message);
	}

	/**
	 * 判断是否成功： 依据 Wrapper.SUCCESS_CODE == this.code
	 *
	 * @return code =200,true;否则 false.
	 */
	@JsonIgnore
	public boolean success() {
		return Wrapper.SUCCESS_CODE == this.code;
	}

	/**
	 * 判断是否成功： 依据 Wrapper.SUCCESS_CODE != this.code
	 *
	 * @return code !=200,true;否则 false.
	 */
	@JsonIgnore
	public boolean error() {
			return !success();
		}

}
